using System.Collections;

namespace VCDepend.Engine
{
	public class DependencyGraphBuilder
	{
		private ArrayList m_sources;

		public DependencyGraphBuilder()
		{
			m_sources = new ArrayList();
		}

		public void AddDependencySource(IDependencySource source)
		{
			m_sources.Add(source);
		}

		public DependencyGraph BuildGraph(ProjectStore projects)
		{
			DependencyGraph graph = new DependencyGraph();

			foreach (Project project in projects)
			{
				graph.AddNode(project.Name);
				AddDependencies(graph, project);
			}

			return graph;
		}

		private void AddDependencies(DependencyGraph graph, Project project)
		{
			foreach (IDependencySource source in m_sources)
			{
				string[] dependencies = source.GetDependencyNames(project);
				foreach (string dependency in dependencies)
				{
					graph.AddDependency(project.Name, dependency);
				}
			}
		}
	}
}